<%@page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page language="java" import="java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	
	<style type="text/css">
	
		*{
			font-size: 14px;
			font-family: '微软雅黑';
		}
	
	</style>
</head>
<body>

	<!-- http://localhost:8080/mvn-JavaWeb-Cookie/cookiePersistent.jsp  -->
	<!-- 服务端如果浏览器带cookie过来。我就不需要再给cookie给你了。 -->
	
	<%
		//服务要判断浏览器是否带cookie过来。如果带了cookie。就不需要发cookie过去。
		
		
		//1 拿到所有的cookies 集合   
		
		Cookie[] cookies =request.getCookies();
	
		out.println("cookies=["+cookies+"]");
		out.println("<br/>");
		
		int counter =0;
		if(null != cookies){
			
			for(Cookie c : cookies){
				out.print(c.getName()+": "+c.getValue());
				out.print("<br/>");
				if("school".equals(c.getName())){
					out.print("cookie没有过期....");
					//找到了就不要找
					counter++;
				}
			}
			
		}
		
		
		
		if(null == cookies || counter==0 ){
			
			//null 说明第一次访问
			//0         说明 cookie过期了
			
			String qk="";
			if(null == cookies){
				qk="第1次访问服务器";
			}else{
				qk="说明 cookie过期了....";
			}
			out.print(qk+"生成一个新cookie给到浏览器....");
			//说明我从来没有给你cookie
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
			String date02 =sdf.format(date);
			
			//1 创建1个javax.servlet.http.Cookie类的对象
			Cookie cookie = new Cookie("school","szrt"+date02);
			
			cookie.setMaxAge(20);
			
			//2 调用response的addCookie方法将cookie发送给浏览器
			response.addCookie(cookie);
		}
		
		
		
	
	%>

</body>
</html>